import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('文本组件'),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextWidget(), // 普通文本属性
              SizedBox(height: 10),
              RichTextWidget(), // 富文本
              SizedBox(height: 10),
              DefStyleTextWidget(), // 样式继承
            ],
          )),
    );
  }
}

// 1.文本组件基本使用/属性
class TextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text(
      '普通文本组件' * 10,
      // 最大行数
      maxLines: 1,
      textAlign: TextAlign.center,
      overflow: TextOverflow.ellipsis,
      // 文本放大倍数，默认为1.0
      textScaleFactor: 2,
      style: TextStyle(
        color: Colors.blue,
        fontSize: 12.0,
        background: Paint()..color = Colors.redAccent, // 背景色
        decoration: TextDecoration.underline, // 装饰，下划线
      ),
    );
  }
}

// 2.富文本
class RichTextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text.rich(TextSpan(children: [
      TextSpan(
          text: 'Hello',
          style: TextStyle(
            color: Colors.redAccent,
            fontSize: 12.0,
          )),
      // 富文本图标
      WidgetSpan(
          child: Icon(
        Icons.face,
        color: Colors.red,
      )),
      TextSpan(
          text: 'world',
          style: TextStyle(
            color: Colors.yellow,
            fontSize: 20.0,
          ),
          // TextSpan添加点击事件
          recognizer: TapGestureRecognizer()
            ..onTap = () {
              debugPrint('hello world');
            })
    ]));
  }
}

// 3.默认样式继承
class DefStyleTextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
        style: TextStyle(color: Colors.redAccent, fontSize: 22.0),
        child: Column(
          children: [
            Text('完全继承默认样式'),
            Text(
              '完全不继承默认样式',
              style: TextStyle(inherit: false, color: Colors.yellow), // inherit
            ),
            Text(
              '除color属性继承默认样式',
              style: TextStyle(color: Colors.pink),
            ),
          ],
        ));
  }
}
